package com.hele.sts.action.admin;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.hele.sts.dao.*;
import com.hele.sts.pub.StringUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @ClassName QUserMgtAction
 * @Description TODO
 * @Author xuning
 * @Date 2019-03-17 13:54
 **/
public class QUserMgtAction implements Action {

    static Logger logger = LoggerFactory.getLogger(QUserMgtAction.class);
    static final String ACTION_TO_CREATE = "to_create";
    static final String ACTION_CREATE = "create";

    String errorInfo;
    String action;

    String userId;
    String userName;
    String unit;
    String department;
    String certificate;
    String telephone;
    String physicalAddress;
    String ipAddress;
    double discount;
    double blance;
    String yhlx;
    String fullname;
    //xuning add 20190329
    String spqx;
    String gxdw;

    Collection<Bm> newSpqxList;
    Collection<Bm> newDjjgList;
    //xuning add 20190329 end
    @Autowired
    private XtLogDao xtLogDao;

    @Autowired
    private BmDao bmDao;

    @Override
    public String execute() throws Exception {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        String loginName = (String) session.get("AdminUser.loginName");
        if ((loginName == null) || (loginName.length() < 1)) {
            setErrorInfo("没有登录");
            return "error";
        }
        User adminUser = (User) session.get(("AdminUser"));

        if (ACTION_TO_CREATE.equals(action)) {
            List<Bm> spqxList = bmDao.getBmList(BmDao.TABLE_SPQX);
            final String adminSpqx = adminUser.getSpqx();
            newSpqxList = Collections2.filter(spqxList, new Predicate<Bm>() {
                @Override
                public boolean apply(Bm input) {
                    return input!=null && input.getCode().startsWith(adminSpqx.substring(0,1));
                }
            });
            List<Bm> djjgList = bmDao.getBmList(BmDao.TABLE_DJJG);
            String adminDjjg = adminUser.getDjjg();
            while (adminDjjg.endsWith("00")){
                adminDjjg = adminDjjg.substring(0,adminDjjg.length()-2);
            }
            final String djjgBxj = adminDjjg;
            newDjjgList = Collections2.filter(djjgList, new Predicate<Bm>() {
                @Override
                public boolean apply(Bm input) {
                    return input!=null && input.getCode().startsWith(djjgBxj);
                }
            });
            gxdw = adminUser.getGxdwdm();
            return "to_create";
        }
        if (ACTION_CREATE.equals(action)) {

            //20180926 check the name is unique
            String searchUserId = QEkUserDao.getUseridByUsername(userName);
            if (!"".equals(searchUserId)) {
                setErrorInfo("该用户已存在，请重新开户");
                return "to_create";
            }
            QEkUser qEkUser = createQEkUser();

            while (true) {
                QEkUser qEkUser0 = QEkUserDao.load(this.userId);
                if (qEkUser0 == null) {
                    break;
                }
                this.userId = StringUtil.createUserId();
                qEkUser.setUserId(userId);
            }

            boolean b = QEkUserDao.insert(qEkUser);
            if (!b) {
                xtLogDao.insertLog("业务日志", "后管用户开户失败", adminUser.getUserId(), adminUser.getUserName(), "成功", "用户：" + adminUser.getUserId() + " 内部用户开户：" + userId + " ", this.getClass().getName());
                setErrorInfo("开户操作失败1");
                return "to_create";
            }

            b = QUserDao.insert(createQUser());
            if (!b) {
                xtLogDao.insertLog("业务日志", "后管用户开户失败", adminUser.getUserId(), adminUser.getUserName(), "成功", "用户：" + adminUser.getUserId() + " 内部用户开户：" + userId + " ", this.getClass().getName());
                setErrorInfo("开户操作失败2");
                return "to_create";
            }

            xtLogDao.insertLog("业务日志", "后管用户开户成功", adminUser.getUserId(), adminUser.getUserName(), "成功", "用户：" + adminUser.getUserId() + " 内部用户开户：" + userId + " ", this.getClass().getName());
            return "create_success";
        }
        setErrorInfo("未知方法执行");
        return "error";
    }

    QEkUser createQEkUser() {
        QEkUser bean = new QEkUser();
        bean.setBlance(this.blance);
        bean.setCertificate(this.certificate);
        bean.setDepartment(this.department);
        bean.setDiscount(this.discount);
        bean.setIpAddress(this.ipAddress);
        bean.setPhysicalAddress(this.physicalAddress);
        bean.setTelephone(this.telephone);
        bean.setUnit(this.unit);
        bean.setFullname(this.fullname);
        if ((this.userId == null) || (this.userId.length() < 1)) {
            this.userId = StringUtil.createUserId();
        }
        bean.setUserId(this.userId);
        bean.setUserName(this.userName);
        //xuning add 20150113

        //bean.setDjId(StringUtil.randomStrWithDatetime());

        //xuning add 20190329
        bean.setSpqx(this.spqx);
        bean.setGxdw(this.gxdw);
        return bean;
    }

    QUser createQUser() {
        QUser bean = new QUser();
        bean.setStatus("1");
        bean.setUserId(this.userId);
        bean.setYhlx(this.yhlx);
//        bean.setUserName(this.userName);
//        bean.setRealName(this.fullname);
        return bean;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getErrorInfo() {
        return errorInfo;
    }

    public void setErrorInfo(String errorInfo) {
        this.errorInfo = errorInfo;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getCertificate() {
        return certificate;
    }

    public void setCertificate(String certificate) {
        this.certificate = certificate;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getPhysicalAddress() {
        return physicalAddress;
    }

    public void setPhysicalAddress(String physicalAddress) {
        this.physicalAddress = physicalAddress;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    public double getBlance() {
        return blance;
    }

    public void setBlance(double blance) {
        this.blance = blance;
    }

    public String getYhlx() {
        return yhlx;
    }

    public void setYhlx(String yhlx) {
        this.yhlx = yhlx;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public Collection<Bm> getNewSpqxList() {
        return newSpqxList;
    }

    public void setNewSpqxList(Collection<Bm> newSpqxList) {
        this.newSpqxList = newSpqxList;
    }

    public Collection<Bm> getNewDjjgList() {
        return newDjjgList;
    }

    public void setNewDjjgList(Collection<Bm> newDjjgList) {
        this.newDjjgList = newDjjgList;
    }

    public String getSpqx() {
        return spqx;
    }

    public void setSpqx(String spqx) {
        this.spqx = spqx;
    }

    public String getGxdw() {
        return gxdw;
    }

    public void setGxdw(String gxdw) {
        this.gxdw = gxdw;
    }
}
